﻿using System.Collections.Generic;
using SpCop.Core.Model;
using SpCop.Model;

namespace SpCop.General.Scopes
{
    public class RespectElementScopes : RuleBase
    { 
        private static readonly Dictionary<ElementType, Scope> ElementScopes = new Dictionary<ElementType, Scope>();
        const Scope All = Scope.All;
        private const Scope SiteWeb = Scope.Site|Scope.Web;
        private IList<Problem> _problems;

        static RespectElementScopes()
        {
                // Site
                ElementScopes.Add(ElementType.ContentType, Scope.Site);
                ElementScopes.Add(ElementType.Field, Scope.Site);

                // Site And Web
                ElementScopes.Add(ElementType.Module, SiteWeb);
                ElementScopes.Add(ElementType.ListInstance,  SiteWeb);
                ElementScopes.Add(ElementType.ListTemplate,  SiteWeb);


                // All Scopes
                ElementScopes.Add(ElementType.Receivers, All);
                ElementScopes.Add(ElementType.FeatureSiteTemplateAssociation,All);
                ElementScopes.Add(ElementType.Control, All);
                ElementScopes.Add(ElementType.CustomAction, All);
                ElementScopes.Add(ElementType.CustomActionGroup, All);
           
        }

       
        
        public override IList<Model.Problem> Check(Package package)
        {
            _problems =  base.Check(package);
            VisitCollection(package.Features,Visit);
            return _problems;
        }
        public override void Visit(Element item)
        {
            //var isallowed = ElementScopes[item.ElementType].HasFlag(item.Feature.Scope);
            var isallowed = true; //TODO: Fix this.
            if (!isallowed)
               AddProblem("WrongScope", item.ElementType.ToString(), item.Feature.Title);

            base.Visit(item);
        }

       
    }
}
